package com.njupt.swg.threadstudyjimin.item06.immutable;

import com.google.common.collect.Maps;
import com.njupt.swg.threadstudyjimin.annotation.ThreadSafe;

import java.util.Collections;
import java.util.Map;

@ThreadSafe
public class ImmutableExample2 {
    private static Map<Integer,Integer> map = Maps.newHashMap();

    static {
        map.put(1,2);
        map.put(3,4);
        map.put(5,6);
        map = Collections.unmodifiableMap(map);//处理过的map就不能再被修改了，否则报运行时异常：java.lang.UnsupportedOperationException
    }

    public static void main(String[] args) {
        map.put(1,3);   //这个put会引发异常
        System.out.println(map.get(1));
    }
}
